/**
 * 向后端请求用户的菜单，动态生成路由
 */
import { constantRouterMap,asyncRouterMap } from '@/config/router.config'
import { generatorDynamicRouter } from '@/router/generator-routers'
import router from '@/router'

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)


    }
  },
  actions: {
    GenerateRoutes ({ commit }, menu) {
      return new Promise(resolve => {
        generatorDynamicRouter(menu).then(routers => {
          commit('SET_ROUTERS', routers)
          console.log(routers)
          routers.forEach(r => {
            router.addRoute(r)
          })
          resolve()
        })
      })
    }
  }
}

export default permission
